<template>
  <div class="roles">
    <div class="content">
      <!-- <el-input class="input" v-model="id" placeholder="请输入内容"></el-input> -->
      <br />
      <el-input
        class="input"
        v-model="name"
        placeholder="请输入name"
      ></el-input>
      <el-input
        class="input"
        v-model="age"
        type="number"
        placeholder="请输入age"
      ></el-input>
      <el-input
        class="input"
        v-model="email"
        placeholder="请输入email"
      ></el-input>

      <br />
      <el-button type="success" @click="searchUser">查询</el-button>
      <el-button type="primary" @click="addUser">添加</el-button>
    </div>

    <el-table :data="tableData" style="width: 100%">
      <el-table-column prop="id" label="id" width="60"> </el-table-column>
      <el-table-column prop="name" label="姓名" width="180"> </el-table-column>
      <el-table-column prop="age" label="年龄" width="180"> </el-table-column>
      <el-table-column prop="email" label="邮箱"> </el-table-column>
    </el-table>
  </div>
</template>

<script>
let ip = "47.103.157.137:8081";
//let ip = '192.168.4.107:8081'
export default {
  name: "login",
  data() {
    return {
      id: "",
      name: "",
      age: "",
      email: "",
      tableData: [],
    };
  },
  methods: {
    addUser() {
      let user = {
        name: "",
        age: "",
        email: "",
      };

      user.name = this.name;
      user.age = this.age;
      user.email = this.email;

      this.$axios
        .post(`http://${ip}/users`, user)
        .then((data) => {
          console.log(data);

          this.searchUser();
        })
        .catch((err) => {
          console.log(err);
        });
    },
    searchUser() {
      this.$axios.get(`http://${ip}/users`).then((data) => {
        this.tableData = data.data;
      });
    },
  },
};
</script>

<style>
.roles {
  height: 100%;
}
.content {
  width: 50%;
  margin: 0 auto;
}
.input {
  width: 500px;
  margin-bottom: 30px;
}
</style>
